//
//  JZBBookViewController.m
//  JZB
//
//  Created by liujianlong on 2022/8/31.
//

#import "JZBBookViewController.h"
#import "JZBHeader.h"
#import "JZBBookCell.h"
#import "JZBAddPayViewController.h"
#import "JZBDateViewController.h"

@interface JZBBookViewController ()
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) JZBHeader *header;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *collectionViewTop;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, assign) AddStyle type;
@property (nonatomic, assign) int currentStamp;
@property (nonatomic, assign) int selectStamp;
@end

@implementation JZBBookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)initUI{
    self.backBtn.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    self.header = [JZBHeader loadFromXIB];
    self.header.title.text = LS(@"快速记账本");
    self.type = AddStylePay;
    WS(self)
    self.header.block = ^(int tag) {
        [weakself action:tag];
    };
    self.header.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.header];
    NSLayoutConstraint *top = [self.header a1:NSLayoutAttributeTop e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeTop c:0];
    NSLayoutConstraint *left = [self.header a1:NSLayoutAttributeLeft e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeLeft c:0];
    NSLayoutConstraint *height = [self.header a1:NSLayoutAttributeHeight e:NSLayoutRelationEqual t:nil a2:NSLayoutAttributeNotAnAttribute c:120];
    NSLayoutConstraint *right = [self.header a1:NSLayoutAttributeRight e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeRight c:0];
    [self.view addConstraints:@[top,left,height,right]];
    
    self.collectionViewTop.constant = self.header.bounds.size.height;
    [self.collectionView registerNib:[JZBBookCell nib] forCellWithReuseIdentifier:[JZBBookCell ID]];
}
- (NSString *)week:(int)week{
    switch (week) {
        case 1:
            return LS(@"周日");
        case 2:
            return LS(@"周一");
        case 3:
            return LS(@"周二");
        case 4:
            return LS(@"周三");
        case 5:
            return LS(@"周四");
        case 6:
            return LS(@"周五");
        case 7:
            return LS(@"周六");
        default:
            return @"";;
    }
}
- (void)action:(int)tag{
    if (tag == 201) {
        JZBDateViewController *vc = [JZBDateViewController new];
        vc.selectBlock = ^(int stamp) {
            self.selectStamp = stamp;
        };
        [self presentViewController:vc animated:NO completion:nil];
    }else if (tag == 202){
        self.type = AddStylePay;
    }else if (tag == 203){
        self.type =  AddStyleIncome;
    }
}
- (void)setType:(AddStyle)type{
    _type = type;
    self.array = type == AddStylePay ? @[@"餐饮",@"购物",@"日用",@"交通",@"零食",@"水果",@"烟酒",@"健康",@"缴费",@"娱乐",@"服饰",@"租借",@"美容",@"宠物",@"汽车",@"礼金",@"旅游",@"学习",@"办公",@"数码"] : @[@"工资",@"奖金",@"副业",@"理财",@"股票",@"结款",@"分红",@"其他"];
    [self.collectionView reloadData];
}
- (int)currentStamp{
    return self.selectStamp != 0 ? self.selectStamp : [[NSDate currentTimeStamp:@"yyyy/MM/dd hh:mm:ss"] intValue];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  self.array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    JZBBookCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[JZBBookCell ID] forIndexPath:indexPath];
    cell.img.image = [UIImage imageNamed:self.array[indexPath.row]];
    cell.content.text = LS(self.array[indexPath.row]);
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    JZBAddPayViewController *vc = [JZBAddPayViewController new];
    vc.style = self.type;
    NSDateComponents *date = [NSDate dateOfStamp:self.currentStamp];
    vc.model = [JZBBook new];
    vc.model.type = self.array[indexPath.row];
    vc.model.stamp = self.currentStamp;
    vc.model.year = [NSString stringWithFormat:@"%d",(int)date.year];
    vc.model.month = [NSString stringWithFormat:@"%d",(int)date.month];
    vc.model.day = [NSString stringWithFormat:@"%d",(int)date.day];
    vc.model.week = [NSString stringWithFormat:@"%@",[self week:(int)date.weekday]];
    vc.model.userID = APP.user.ID;
    vc.model.ispay = self.type == AddStylePay;
    [self.navigationController pushViewController:vc animated:YES];
    self.selectStamp = 0;
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake(50,80);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(15, 15, 15, 15);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}
@end
